﻿using System;
using System.Collections.Generic;
using Gp.Input;
using QFramework;
using InputContext = UnityEngine.InputSystem.InputAction.CallbackContext;

namespace Gp.Scripts.Core
{
    public static class GameStateExtenstion
    {
        public static void SwitchState(this GameState self, GameState newState)
        {
            Global.Get<BattleSystem>().BattleSettle.GameState = newState;
        }
    }


    public enum SelectEventType
    {
        HIGHLIGHT = 1,
        DE_HIGHLIGHT = 2,
        SELECT = 3,
        DESELECT = 4,
    }
    
    

    /// <summary>
    /// 游戏状态
    /// </summary>
    public abstract class GameState
    {
        protected readonly BattleSettleRoot BattleSettle;

        protected GameState()
        {
            BattleSettle = Global.Get<BattleSystem>().BattleSettle;
        }

        /// <summary>
        /// 用于重定向状态。
        /// </summary>
        /// <param name="nextState"> 即将切换至的下一个状态 </param>
        /// <returns>重定向的状态</returns>
        public virtual GameState OnMakeTransition(GameState nextState)
        {
            return nextState;
        }


        public void StateEnter(GameState lastState)
        {
            OnStateEnter(null);
        }
        protected virtual void OnStateEnter(GameState lastState) { }


        public void StateExit(GameState nextState)
        {
            _unRegisters?.ForEach(x => x.UnRegister());
            OnStateExit(nextState);
        }
        protected virtual void OnStateExit(GameState nextState) { }


        // public virtual void OnEntityDeselect(BaseEntity entity) { }

        public virtual void OnEntitySelectEvent(BaseEntity entity, SelectEventType eventType)
        {
            switch (eventType)
            {
                case SelectEventType.HIGHLIGHT:
                    entity.MarkHighlight();
                    break;
                case SelectEventType.DE_HIGHLIGHT:
                    entity.MarkDeHighlight();
                    break;
            }
        }

        public virtual void OnVCellSelectEvent(VBattleCell cell, SelectEventType eventType) { }

        // public virtual void OnEntityHighlight(BaseEntity entity)
        // {
        //     entity.MarkHighlight();
        // }
        //
        // public virtual void OnEntityDeHighlight(BaseEntity entity)
        // {
        //     entity.MarkDeHighlight();
        // }
        
        
        

        private List<IUnRegister> _unRegisters;

        public IUnRegister RegisterEvent<T>(Action<T> callBack)
        {
            _unRegisters ??= new List<IUnRegister>();
            var unRegister = MainArchitecture.Interface.RegisterEvent(callBack);
            _unRegisters.Add(unRegister);
            return unRegister;
        }

        public void UnRegisterEvent<T>(Action<T> callBack)
        {
            MainArchitecture.Interface.UnRegisterEvent(callBack);
        }

        public IUnRegister BindInput(InputMapCode code, Action<InputContext> action, InputTriggerType type = InputTriggerType.Started)
        {
            _unRegisters ??= new List<IUnRegister>();
            var unRegister = Global.Get<InputManager>().Bind(code, action, type);
            _unRegisters.Add(unRegister);
            return unRegister;
        }

        public void UnBindInput(InputMapCode code, Action<InputContext> action, InputTriggerType type = InputTriggerType.Started)
        {
            Global.Get<InputManager>().UnBind(code, action, type);
        }
    }
}